package com.pd.modules.client.database;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Observable;

import com.pd.modules.client.Client;
import com.pd.modules.common.DateInterval;
import com.pd.modules.common.Message;
import com.pd.modules.common.MessageType;
import com.pd.modules.common.Reservation;
import com.pd.modules.common.Settings;

public class DBReservationsHandler extends Observable
{
	String mRoomFilter = null;
	DateInterval mDateFilter = null;
	Reservation[] mReservations = null;

	public synchronized Reservation[] getAllReservations()
	{
		return mReservations;
	}

	public synchronized void clear()
	{
		clearAllFilters();
		mReservations = null;
		dataChanged();
	}

	private void dataChanged()
	{
		setChanged();
		notifyObservers();
	}

	public DateInterval getDateFilter()
	{
		return mDateFilter;
	}

	public String getRoomFilter()
	{
		return mRoomFilter;
	}

	public boolean dateFilterContains(Date _date)
	{
		return mDateFilter == null ? false : mDateFilter.contains(_date);
	}

	public boolean roomFilterIs(String _roomID)
	{
		return mRoomFilter == null ? false : mRoomFilter.equals(_roomID);
	}

	public synchronized void setData(Reservation[] _newData)
	{
		mReservations = _newData;
		dataChanged();
	}

	public synchronized void setDateFilter(DateInterval _newDateFilter)
	{
		mDateFilter = _newDateFilter;
		refresh();
	}

	public synchronized void setRoomFilter(String _roomFilter)
	{
		mRoomFilter = _roomFilter;
		refresh();
	}

	public void refresh()
	{
		if (null == mRoomFilter)
		{
			setData(null);
			return;
		}

		Message tmpMessage = null;
		tmpMessage = new Message(MessageType.ListRoomReservations);
		tmpMessage.addData(mRoomFilter);

		if (null != mDateFilter)
		{
			DateFormat formatter = new SimpleDateFormat(Settings.DATE_HOUR_FORMAT);
			tmpMessage.addData(formatter.format(mDateFilter.getStartAsDate()));
			tmpMessage.addData(formatter.format(mDateFilter.getEndAsDate()));
		}

		Client.getInstance().sendMessageToServer(tmpMessage, true);
	}

	public synchronized void clearAllFilters()
	{
		mDateFilter = null;
		mRoomFilter = null;
		refresh();
	}

	public void deleteReservation(String _id)
	{
		Message tmpMessage = new Message(MessageType.DeleteRoomReservation);
		tmpMessage.addData(_id);

		Client.getInstance().sendMessageToServer(tmpMessage, true);
	}

	public void parseDataFromMessage(Message _message)
	{
		if (_message.getDataSize() == 0)
			setData(null);

		Reservation[] tmpArray = new Reservation[_message.getDataSize()];
		for (int i = 0; i < tmpArray.length; i++)
		{
			String asFileEntry = _message.getData(i, String.class);
			tmpArray[i] = new Reservation(asFileEntry);
		}

		setData(tmpArray);
	}



	public synchronized void createRoomReservation(String _roomID, DateInterval _interval, String _username)
	{
		DateFormat formatter = new SimpleDateFormat(Settings.DATE_HOUR_FORMAT);
		String starHour = formatter.format(_interval.getStartAsDate());
		String endHour = formatter.format(_interval.getEndAsDate());
		
		// Constroi a mensagem
		Message tmpMessage = new Message(MessageType.CreateRoomReservation);
		tmpMessage.addData(_roomID);
		tmpMessage.addData(starHour);
		tmpMessage.addData(endHour);
		
		// Finalmente envia o pedido ao servidor
		Client.getInstance().sendMessageToServer(tmpMessage, true);
	}
}
